#ifndef PUBLICDATACLASS_H
#define PUBLICDATACLASS_H

#include <QStringList>
#include <QString>
#include <QSqlQuery>

static QStringList UserTypeStr ={
"isAdmin",
"isCounselor",
"isTeacher",
"isStudent"
};

static QStringList UserTypeStrChinese ={
"管理员",
"辅导员",
"教师",
"学生"
};

class DB_AbstractOperation{
public:
    virtual bool insertOne() = 0;
    virtual bool deleteOne() = 0;
    virtual bool updateOne() = 0;
    virtual bool fromQuery(QSqlQuery& q) = 0;
    virtual bool exec(QSqlQuery &query);

};


class DB_userInfo :public DB_AbstractOperation
{
public:
    int account = -1;
    QString name;
    QString pwd;
    int isAdmin = 0;
    int isCounselor = 0;
    int isTeacher = 0;
    int isStudent = 0;
    int class_id  = -1;

    int userType = -1;

    // DB_AbstractOperation interface
public:
    virtual bool insertOne() override;
    virtual bool deleteOne() override;
    virtual bool updateOne() override;

    DB_userInfo(){}
    DB_userInfo(QSqlQuery& q){fromQuery(q);}
    virtual bool fromQuery(QSqlQuery &q) override{
        bool isOk = false;

        account = q.value("account").toInt(&isOk);
        isAdmin = q.value("isAdmin").toInt();
        isCounselor = q.value("isCounselor").toInt();
        isTeacher = q.value("isTeacher").toInt();
        isStudent = q.value("isStudent").toInt();
        class_id = q.value("class_id").toInt();

        name = q.value("name").toString();
        pwd = q.value("pwd").toString();

        return isOk;
    }
};



class DB_View_ClassInfo{
public:
    int classID;
    QString className;
    int consultantID;
    int courseID;
    QString courseName;
    double credits;
    int TAccount;
    QString TName;

    DB_View_ClassInfo(QSqlQuery& q){fromQuery(q);}
    bool fromQuery(QSqlQuery& q){
        bool isOk = false;
        classID = q.value("classID").toInt(&isOk);
        courseID = q.value("courseID").toInt();
        consultantID = q.value("consultantID").toInt();
        TAccount = q.value("TAccount").toInt();
        className = q.value("className").toString();
        courseName = q.value("courseName").toString();
        TName = q.value("TName").toString();
        credits = q.value("credits").toDouble();

        return isOk;
    }
};

class DB_View_StudentGrades{
public:
    int id;
    QString name;
    int classID;
    int courseID;
    QString coureName;
    double credit;
    double grade;
    double GPA;
    int rankingInClass;

    DB_View_StudentGrades(){}
    DB_View_StudentGrades(QSqlQuery& q){fromQuery(q);}
    bool fromQuery(QSqlQuery& q){
        bool isOk = false;

        id = q.value("id").toInt(&isOk);
        courseID = q.value("courseID").toInt();
        classID = q.value("classID").toInt();
        rankingInClass = q.value("rankingInClass").toInt();

        name = q.value("name").toString();
        coureName = q.value("coureName").toString();

        credit = q.value("credit").toDouble();
        grade = q.value("grade").toDouble();
        GPA = q.value("GPA").toDouble();

        return isOk;
    }
};

class DB_View_StudentInfo{
public:
    int id;
    QString name;
    int classID;
    QString className;
    int consultantID;
    QString consultantName;
    double avgGPA;
    int rank;

    DB_View_StudentInfo(QSqlQuery& q){fromQuery(q);}
    bool fromQuery(QSqlQuery& q){
        bool isOk = false;

        id = q.value("id").toInt(&isOk);
        classID = q.value("classID").toInt();
        consultantID = q.value("consultantID").toInt();
        rank = q.value("rank").toInt();

        name = q.value("name").toString();
        className = q.value("className").toString();
        consultantName = q.value("consultantName").toString();

        avgGPA = q.value("avgGPA").toDouble();
        return isOk;
    }
};

class DB_CreateClassInfo{

public:
    struct T_Info{
        int id;
        QString name;

        T_Info(QSqlQuery& q){fromQuery(q);}
        bool fromQuery(QSqlQuery& q){
            bool isOk = false;
            id = q.value("id").toInt(&isOk);
            name = q.value("name").toString();
            return isOk;
        }
    };


    QList<T_Info> professionals;
    QList<T_Info> teachers;
    QList<T_Info> courses;
    QList<T_Info> counselors;

    QList<T_Info>& data(int i){
        QList< QList<T_Info>* > data = {&professionals,&teachers,&courses,&counselors};
        return *data.at(i);

    }
};




#endif // PUBLICDATACLASS_H
